package com.share.device.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.exception.ServiceException;
import com.share.device.domain.PowerBank;
import com.share.device.mapper.PowerBankMapper;
import com.share.device.service.IPowerBankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PowerBankServiceImpl extends ServiceImpl<PowerBankMapper, PowerBank> implements IPowerBankService {

    /**
     * 查询充电宝列表
     */
    @Override
    public List<PowerBank> selectPowerBankList(PowerBank powerBank) {
        LambdaQueryWrapper<PowerBank> wrapper = new LambdaQueryWrapper<>();

        // 充电宝编号
        String powerBankNo = powerBank.getPowerBankNo();
        // 电量
        BigDecimal electricity = powerBank.getElectricity();
        // 描述
        String description = powerBank.getDescription();
        // 状态（0:未投放 1：可用 2：已租用 3：充电中 4：故障）
        String status = powerBank.getStatus();

        if (StrUtil.isNotEmpty(powerBankNo)) {
            wrapper.eq(PowerBank::getPowerBankNo, powerBankNo);
        }

        if (electricity != null) {
            wrapper.eq(PowerBank::getElectricity, electricity);
        }

        if (StrUtil.isNotEmpty(description)) {
            wrapper.eq(PowerBank::getDescription, description);
        }

        if (StrUtil.isNotEmpty(status)) {
            wrapper.eq(PowerBank::getStatus, status);
        }

        wrapper.eq(PowerBank::getDelFlag, "0");

        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据充电宝编号查询充电宝
     */
    @Override
    public PowerBank getByPowerBankNo(String powerBankNo) {
        return baseMapper.selectOne(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBankNo));
    }

    /**
     * 新增充电宝
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int savePowerBank(PowerBank powerBank) {
        long count = this.count(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo()));

        if (count > 0) {
            throw new ServiceException("该充电宝编号已存在");
        }

        baseMapper.insert(powerBank);

        return 1;
    }

    /**
     * 修改充电宝
     */
    @Override
    public int updatePowerBank(PowerBank powerBank) {
        // 获取旧的数据
        PowerBank oldPowerBank = this.getById(powerBank.getId());
        if (null != oldPowerBank && !"0".equals(oldPowerBank.getStatus())) {
            throw new ServiceException("该充电宝已投放，无法修改");
        }

        // 判断柜机编号是否改变
        if (!oldPowerBank.getPowerBankNo().equals(powerBank.getPowerBankNo())) {
            long count = this.count(
                    new LambdaQueryWrapper<PowerBank>()
                            .eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo())
            );
            if (count > 0) {
                throw new ServiceException("该充电宝编号已存在");
            }
        }

        this.updateById(powerBank);

        return 1;
    }
}
